package com.barry.generator

import java.io.{File, PrintWriter}
import java.util
import java.util.UUID

import com.barry.dto.{ReportConfig, ReportOutput}
import com.barry.utils.{AnnotationUtil, MathUtils, ScalaFunction, ZipUtil}

import scala.collection.JavaConverters._

/**
  * 类功能描述：多个CSV报表打包成Zip
  *
  * @author WangXueXing create at 18-11-12 下午2:58
  * @version 1.0.0
  */
trait CsvZipReportAction extends ReportAction {
  /**
    * 根据数据生成报表
    *
    * @return
    */
  override def generateReport(reportId: Long, reportConfig: ReportConfig): ReportOutput = {
    val fileList: util.ArrayList[File] = new util.ArrayList[File]
    getReportData.foreach { x =>
      val file = File.createTempFile(x._1 + "_" + MathUtils.getRandom(4), ".csv")
      ScalaFunction.tryWithResources(new PrintWriter(file, "GBK")) { out =>
        val dataObj = x._2.isInstanceOf[Seq[AnyRef@unchecked]] match {
          case true => x._2.asInstanceOf[List[AnyRef@unchecked]]
          case _ => List.empty
        }
        //根据注解顺序生成CSV数据列表
        AnnotationUtil.getValueWithHead(dataObj).foreach(println(_))
      }
      fileList.add(file)
    }
    val localFile = File.createTempFile(UUID.randomUUID().toString, ".zip")
    ZipUtil.zipFiles(fileList, localFile)
    fileList.asScala.foreach(_.deleteOnExit())

    //云上传

    //返回云路径
    ReportOutput("")
  }

  /**
    * 设置模板路径
    * @return
    */
  def getReportData: Map[String, AnyRef]
}
